import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Buffer} from "buffer";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";
import {AxdrOctetString} from "../asn1.axdr/AxdrOctetString";



enum Choice {
    _ERR_NONE_SELECTED = -1,
    DAR = 0,
    RET_RESULT = 1,
}

export class TransResult implements AxdrType{

    dataCode : Buffer | null = null;
    choice : Choice = Choice._ERR_NONE_SELECTED;
    dar : AxdrEnum | null = null;
    retResult : AxdrOctetString | null = null;

    constructor() {

    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;
        let choosen = new AxdrEnum();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = choosen.getValue();

        if (this.choice == Choice.DAR) {
            this.dar = new AxdrEnum();
            this.dar.set_const();
            codeLength += this.dar.decode(input);
            return codeLength;
        }else if (this.choice == Choice.RET_RESULT) {
            this.retResult = new AxdrOctetString();
            codeLength += this.retResult.decode(input);
            return codeLength;
        }
        throw new Error("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    encode(output: ReverseByteArrayOutputStream): number {
        if (this.dataCode != null) {
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;

        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Error("Error encoding AxdrChoice: No item in choice was selected.");
        }

        let codeLength = 0;

        if (this.choice == Choice.RET_RESULT) {
            codeLength += this.retResult.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(1);
            codeLength += c.encode(output);
            return codeLength;
        }else if (this.choice == Choice.DAR) {
            codeLength += this.dar.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(0);
            codeLength += c.encode(output);
            return codeLength;
        }
        throw new Error("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    getChoiceIndex(): Choice {
        return this.choice;
    }

    resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.dar = null;
        this.retResult = null;
    }

    setDar(dar : AxdrEnum){
        this.resetChoices();
        this.choice = Choice.DAR;
        this.dar = dar;
    }

    setRetResult(retResult : AxdrOctetString){
        this.resetChoices();
        this.choice = Choice.RET_RESULT;
        this.retResult = retResult;
    }

    toString(): string {
        if (this.choice == Choice.DAR) {
            if (this.dar != null) {
                return "choice: {dar: " + this.dar + "}";
            } else {
                return "choice is dar but dar is null";
            }
        }else if(this.choice == Choice.RET_RESULT){
            if(this.retResult != null){
                return "choice: {retresult: " + this.retResult + "}";
            }else{
                return "choice is retResult but retResult is null";
            }
        }else {
            return "unknown";
        }
    }

}
